<!--
 * @Description: 任务管理
 * @Author: liyujie
 * @Date: 2021/10/27
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/27
-->
<template>
    <PageLayout
        title="任务管理"
        class="standard-task-list-wrapper"
    >
        <template slot="filter">
            <zd-tabs
                :tab-list="tabsList"
                v-model="filterData.task_category"
                style="padding-left: 8px"
                @change="handleTabChange()"
            />
            <div class="filter-view">
                <div class="left">
                    <div class="filter-item">
                        <div class="filter-item__title">
                            任务标题
                        </div>
                        <el-input
                            placeholder="搜索任务标题"
                            v-model="filterData.keyword"
                            suffix-icon="el-icon-search"
                            clearable
                            size="medium"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <p class="filter-item__title">
                            商家名称
                        </p>
                        <el-input
                            placeholder="搜索商家名称"
                            v-model="filterData.shop_title"
                            suffix-icon="el-icon-search"
                            clearable
                            size="medium"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <p class="filter-item__title">
                            任务状态
                        </p>
                        <el-select
                            v-model="filterData.apply_status"
                            filterable
                            placeholder="请选择"
                            size="medium"
                            @change="getTableData(1)"
                        >
                            <el-option
                                v-for="item in statusList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </div>
                    <div class="filter-item">
                        <p class="filter-item__title">
                            上架状态
                        </p>
                        <el-select
                            v-model="filterData.task_status"
                            filterable
                            placeholder="请选择"
                            size="medium"
                            @change="getTableData(1)"
                        >
                            <el-option
                                label="全部"
                                :value="99"
                            />
                            <el-option
                                label="已上架"
                                :value="1"
                            />
                            <el-option
                                label="未上架"
                                :value="0"
                            />
                        </el-select>
                    </div>
                    <div class="filter-item">
                        <p class="filter-item__title">
                            任务时间
                        </p>
                        <el-date-picker
                            v-model="dateTimeRange"
                            type="datetimerange"
                            range-separator="-"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :default-time="['00:00:00', '23:59:59']"
                            size="medium"
                            style="width: 360px"
                            value-format="timestamp"
                            @change="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <el-button
                            type="primary"
                            size="small"
                            class="query-button"
                            @click="getTableData(1)"
                        >
                            查询
                        </el-button>
                    </div>
                </div>
                <div class="right">
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-setting"
                        @click="handleConfig"
                    >
                        全局配置
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-plus"
                        @click="handleRoute(filterData.task_category === 7 ? 'standardTaskRecommendEdit' : 'standardTaskQuestionnaireEdit', {
                            task_no: '',
                            type: 'add'
                        },'auth_save')"
                    >
                        新建任务
                    </el-button>
                </div>
            </div>
        </template>
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="60"
                type="index"
                key="0"
            />
            <el-table-column
                label="任务名称"
                prop="task_title"
                key="1"
            />
            <el-table-column
                label="商家名称"
                prop="shop_title"
                key="2"
                :formatter="emptyFormatter"
            />
            <el-table-column
                :label="filterData.task_category === 7 ? '奖励设置' : '单任务酬劳'"
                align="center"
                key="3"
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.task_category === 7">

                        <el-popover
                            placement="bottom"
                            width="640"
                            trigger="click"
                        >
                            <div style="text-align: center;margin: 12px 0 24px;font-size: 16px">查看奖励设置</div>
                            <el-table :data="scope.row.award_config">
                                <el-table-column
                                    width="150"
                                    label="梯度等级"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        梯度{{ scope.$index + 1 }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    property="require_submit_count"
                                    label="要求数量"
                                    align="center"
                                />
                                <el-table-column
                                    label="奖励类型"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        {{ scope.row.type === 1 ? '现金奖励' : '0元购学生券' }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="奖励金额/学生券数量"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        {{ scope.row.total_num }}{{ scope.row.type === 1 ? '元' : '张' }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="用户端显示金额"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        <div v-if="scope.row.type === 1">{{ scope.row.num }}元</div>
                                        <div v-else>--</div>
                                    </template>
                                </el-table-column>
                            </el-table>
                            <el-button
                                slot="reference"
                                type="text"
                            >查看设置</el-button>
                        </el-popover>
                    </template>
                    <template v-else>
                        {{ scope.row.award_config }}
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                label="领取/总数"
                prop="task_title"
                align="center"
                key="4"
            >
                <template slot-scope="{row}">
                    {{ row.accept_count }} / {{ row.all_count === -1 ? '不限' : row.all_count }}
                </template>
            </el-table-column>
            <el-table-column
                label="领取时间"
                width="180"
                key="5"
            >
                <template slot-scope="scope">
                    {{ scope.row.apply_start_time * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.apply_end_time * 1000 | dateFormatter('yyyy/MM/dd') }}
                </template>
            </el-table-column>
            <el-table-column
                label="任务状态"
                show-overflow-tooltip
                align="center"
                key="6"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot', scope.row.apply_status === 1 ? 'success' : 'normal']"/>
                        <div>{{ scope.row.apply_status | statusFilter }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="上架状态"
                show-overflow-tooltip
                align="center"
                key="7"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.task_status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="setTaskStatus($event, scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                width="180"
                fixed="right"
                key="8"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('standardTaskReceiveList', {
                                task_no: scope.row.task_no,
                                task_category: scope.row.task_category
                            })"
                        >
                            查看报名
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute(scope.row.task_category === 7 ? 'standardTaskRecommendEdit' : 'standardTaskQuestionnaireEdit', {
                                task_no: scope.row.task_no,
                                type: 'edit'
                            }, 'auth_save')"
                        >
                            修改
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute(scope.row.task_category === 7 ? 'standardTaskRecommendEdit' : 'standardTaskQuestionnaireEdit', {
                                task_no: scope.row.task_no,
                                type: 'copy'
                            })"
                        >
                            复制
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute(scope.row.task_category === 7 ? 'standardTaskRecommendStatistics' : 'standardTaskQuestionnaireStatistics', {
                                task_no: scope.row.task_no
                            }, scope.row.task_category === 7 ? 'auth_data_rec' : 'auth_data_qs')"
                        >
                            分析下载
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            style="color: #F56C6C"
                            @click="handleDelete(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                v-if="page.total_count"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import {standardConfigDialog} from '@/components/standard-task/standard-config-dialog';
import ZdTabs from "@/components/zd-tabs";

export default {
    name: 'StandardTaskList',
    components: {ZdTabs},
    mixins: [back],
    data() {
        return {
            filterData: {
                task_category: 7,
                keyword: '',
                shop_title: '',
                apply_status: 99,
                task_status: 99,
                apply_start_time: '',
                apply_end_time: ''
            },
            dateTimeRange: null,
            tabsList: [
                {label: '推广引流', value: 7},
                {label: '问卷调研', value: 8},
            ],
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: 99},
                {label: '未开始', value: 0},
                {label: '进行中', value: 1},
                {label: '已结束', value: 2},
            ],
            authResult: {}
        };
    },
    computed: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '未开始';
                case 1:
                    return '进行中';
                case 2:
                    return '已结束';
                default:
                    return '--';
            }
        }
    },
    mounted() {
        let filterData = JSON.parse(sessionStorage.getItem('STANDARD_TASK_QUERY'));

        if (filterData) {
            this.filterData = filterData;
            this.page.now_page = Number(sessionStorage.getItem('STANDARD_TASK_PAGE_NO'));
            this.dateTimeRange = JSON.parse(sessionStorage.getItem('STANDARD_TASK_DATE'));
            sessionStorage.removeItem('STANDARD_TASK_QUERY');
            sessionStorage.removeItem('STANDARD_TASK_PAGE_NO');
            sessionStorage.removeItem('STANDARD_TASK_DATE');
        }
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '平台标准任务'}, {title: '任务管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {
        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            params.apply_start_time = this.dateTimeRange ? this.dateTimeRange[0] / 1000 : '';
            params.apply_end_time = this.dateTimeRange ? this.dateTimeRange[1] / 1000 : '';
            this.$post('/student/task_stuagent%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         * @param auth_name
         */
        handleRoute(routeName, query, auth_name) {
            if(auth_name && !this.authResult[auth_name]) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$router.push({
                name: routeName,
                query
            });

            sessionStorage.setItem('STANDARD_TASK_QUERY', JSON.stringify(this.filterData));
            sessionStorage.setItem('STANDARD_TASK_PAGE_NO', this.page.now_page);
            sessionStorage.setItem('STANDARD_TASK_DATE', JSON.stringify(this.dateTimeRange));
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 设置任务状态
         * @param val
         * @param row
         */
        setTaskStatus(val, row) {
            let params = {
                task_no: row.task_no,
                task_status: val
            };

            this.$post('/student/task_stuagent%5Cset_status', params, resp => {
                if(resp.code === 1) {
                    this.$notify({
                        title: '成功',
                        message: '设置成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 删除
         * @param row
         */
        handleDelete(row) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            let params = {
                task_no: row.task_no
            };

            this.$zdConfirm('是否要删除此任务?','提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * tab切换
         */
        handleTabChange() {
            this.getTableData(1);
            this.$nextTick(() => {
                this.$refs.table.doLayout();
            });
        },

        /**
         * 全局配置
         */
        async handleConfig() {
            let auth_name = this.filterData.task_category === 7 ? 'auth_config_rec' : 'auth_config_qs';

            if(!this.authResult[auth_name]) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            await standardConfigDialog({
                type: this.filterData.task_category
            });
        }
    }
};
</script>

<style lang="less" scoped>
.standard-task-list-wrapper {
    /deep/ .content-wrapper {
        .header {
            display: none;
        }

        .filter-wrapper {
            display: initial !important;
            margin-top: 0 !important;
            padding: 0 !important;

            .filter-view {
                display: flex;
                padding: 0 24px;
                margin-top: 24px;

                .left {
                    display: flex;
                    flex-wrap: wrap;
                    flex: 1;
                }

                .right {
                    width: 260px;
                    flex-shrink: 0;
                }
            }
        }
    }
}

</style>
